//
//  LAGAnnotationView.m
//  LAMapBasicDemo
//
//  Created by 罗亮富 on 2017/7/19.
//  Copyright © 2017年 2Bulu. All rights reserved.
//


#import "LAGAnnotationView.h"
#import "LAGClusterAnnotation.h"

@implementation LAGAnnotationView
{
#if TARGET_OS_IPHONE
    __weak UILabel *_bageLabel;
#else
    __weak NSTextField *_bageLabel;
#endif
}

-(void)setAnnotation:(id<MKAnnotation>)annotation
{
    [super setAnnotation:annotation];
    if(!annotation)
        return;
    NSUInteger c = 0;
#if !TARGET_OS_IPHONE
    [self setWantsLayer:YES];
#endif
    if([annotation isKindOfClass:[LAGClusterAnnotation class]])
    {
        LAGClusterAnnotation *a = (LAGClusterAnnotation *)annotation;
        c = a.childAnnotations.count;
#if TARGET_OS_IPHONE
        self.backgroundColor = [UIColor redColor];
    }
    else
        self.backgroundColor = [UIColor blueColor];
#else
    self.layer.backgroundColor = [NSColor redColor].CGColor;
}
else{
    
    self.layer.backgroundColor = [NSColor blueColor].CGColor;
}
#endif
    
    [self setBadgeCount:c];
    
}

-(void)setBadgeCount:(NSUInteger)count
{
    if(count == 0)
    {
        [_bageLabel removeFromSuperview];
    }
    else
    {
        _bageLabel.hidden = NO;
        NSString *s = [NSString stringWithFormat:@"%lu",count];
#if TARGET_OS_IPHONE
        if(!_bageLabel)
        {
            UILabel *lb = [[UILabel alloc] init];
            lb.font = [UIFont systemFontOfSize:10.0];
            lb.textColor = [UIColor redColor];
            lb.backgroundColor = [UIColor whiteColor];
            lb.textAlignment = NSTextAlignmentCenter;
            [self addSubview:lb];
            _bageLabel = lb;
        }
        _bageLabel.center = CGPointMake(self.frame.size.width, 0);
        _bageLabel.text = s;
#else
        if(!_bageLabel)
        {
            NSTextField *lb = [[NSTextField alloc] init];
            lb.editable = NO;
            lb.font = [NSFont systemFontOfSize:10.0];
            lb.textColor = [NSColor redColor];
            lb.backgroundColor = [NSColor whiteColor];
            //lb.textAlignment = NSTextAlignmentCenter;
            [self addSubview:lb];
            _bageLabel = lb;
        }
        
        _bageLabel.frame = CGRectMake(0, 0, _bageLabel.frame.size.width, _bageLabel.frame.size.height);
        _bageLabel.stringValue = s;
#endif
        
        NSDictionary *attrDic = @{NSFontAttributeName:_bageLabel.font};
        CGSize size = [s sizeWithAttributes:attrDic];
        
        _bageLabel.frame = CGRectMake(0, 0, size.width+4, size.height+2);
        
    }
}



-(void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
}


@end
